# -*- coding: utf-8 -*-
import urllib, urllib2
import re, sys, os
import xbmcaddon, xbmcgui, xbmcplugin, xbmc

__scriptname__ = "Ansibl TV"
__scriptID__ = "plugin.video.weeb.tv"
__author__ = "weeb.tv"
__url__ = "http://weeb.tv"
__credits__ = ""
__addon__ = xbmcaddon.Addon(__scriptID__)

__language__ = __addon__.getLocalizedString
t = sys.modules[ "__main__" ].__language__

BASE_RESOURCE_PATH = os.path.join( __addon__.getAddonInfo('path'), "resources" )
sys.path.append( os.path.join( BASE_RESOURCE_PATH, "lib" ) )

import main

MENU = {
        
        1: [ t(56005).encode('utf-8'), "5.png" ],
        
}

class Start:
    def __init__(self):
		self.showListOptions()
		
    def showListOptions(self):
        handler = main.Handler()
        parser = main.UrlParser()
        params = parser.getParams()
        mode = parser.getIntParam(params, "mode")
        settings = main.Settings()
        settings.setViewMode('general')
        if mode == None or mode == '':
            self.Categories(MENU)
        elif mode == 1:
            handler.run(5)
        elif mode == 200:
            settings.showSettings()

    def Categories(self, table = {}):
        for num, val in table.items():
            self.addDir(val[0], num, val[1], False, False)
        self.addDir(t(56100).encode('utf-8'), 200, "settings.png", False, False)
        xbmcplugin.endOfDirectory(int(sys.argv[1]))
 
    def addDir(self, name, mode, icon, autoplay, isPlayable = True):
        u = '%s?mode=%d' % (sys.argv[0], mode)
        #u = sys.argv[0] + "?mode=" + str(mode)
        icon = os.path.join( __addon__.getAddonInfo('path'), "images/" ) + icon
        if autoplay or icon == '':
            icon = "DefaultVideo.png"
        liz = xbmcgui.ListItem(name, iconImage = icon, thumbnailImage = icon)
        if autoplay and isPlayable:
            liz.setProperty("IsPlayable", "true")
        liz.setInfo( type = "Video", infoLabels = { "Title": name } )
        xbmcplugin.addDirectoryItem(handle = int(sys.argv[1]), url = u, listitem = liz, isFolder = not autoplay)

init = Start()
